package com.shengruan.user.action;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shengruan.admin.jdbc.PayParameterJdbc;
import com.shengruan.admin.jdbc.UserAccountJdbc;
import com.shengruan.admin.jdbc.UserFavoritesJdbc;
import com.shengruan.admin.vo.PayParameter;
import com.shengruan.admin.vo.Product;
import com.shengruan.admin.vo.User;
import com.shengruan.admin.vo.UserAccount;
import com.shengruan.admin.vo.UserFavorites;
import com.shengruan.common.BaseAction;
import com.shengruan.common.Constants;
import com.shengruan.framework.pager.Pager;
import com.shengruan.index.action.AuctionUtil;

public class UserFavoritesAction extends BaseAction {


    Logger logger = LoggerFactory.getLogger(getClass());

    private UserFavoritesJdbc userFavoritesJdbc = (UserFavoritesJdbc) this.getJdbc(UserFavoritesJdbc.class);
    private PayParameterJdbc payParameterJdbc = (PayParameterJdbc) this.getJdbc(PayParameterJdbc.class);
    private UserAccountJdbc userAccountJdbc = (UserAccountJdbc)this.getJdbc(UserAccountJdbc.class);

    @Override
    public void execute() throws Exception {

        int currentPage = 1;
        try {
            currentPage = Integer.parseInt(this.input("currentPage"));
        } catch (Exception ex) {
        }

        User sessionUser = (User) this.request.getSession().getAttribute(Constants.USER);
        Pager pager = new Pager(currentPage, userFavoritesJdbc.getRows(sessionUser.getId()), 7);
        List<Product> productList = userFavoritesJdbc.findWithPage(currentPage, 7, sessionUser.getId());

        for (Product product : productList) {
            Product productTemp = AuctionUtil.getById(product.getId());
            if (null != productTemp) product = productTemp;
        }

        this.setRequest("pager", pager);
        this.setRequest("productList", productList);
        this.forward("/user/user-favorites.jsp");
    }

    /**
     * 用户收藏
     *
     * @throws Exception
     */
    public void doFavorites() throws Exception {
        String id = this.input("id");
        User sessionUser = (User) this.request.getSession().getAttribute(Constants.USER);
        if (null == sessionUser) {
            this.print("0");
            return;
        }

        int count = userFavoritesJdbc.getRows(sessionUser.getId(), Integer.valueOf(id));
        if (count >= 10) {
            this.print("2");
            return;
        } else if (count < 1) {
            UserFavorites userFavorites = new UserFavorites();
            userFavorites.setUserId(sessionUser.getId());
            userFavorites.setProductId(Integer.valueOf(id));
            userFavorites.setCreationTime(System.currentTimeMillis());
            userFavoritesJdbc.save(userFavorites);

            /*json.put("result", "1");
            json.put("message", "放入购物车成功!");
            this.print(json.toString());
            return;*/

            this.print("3");
            return;
        } else {
            this.print("1");
            return;
        }
    }

    public void delete() throws Exception {
        String id = this.input("id");
        this.userFavoritesJdbc.deleteById(this.getCurrentUser().getId(), Integer.valueOf(id));
    }


    /**
     * 加载购物车
     *
     * @throws Exception
     */
    public void load() throws Exception {
    	User user = this.getCurrentUser();
    	
        if (null != user) {
        	StringBuffer idSb = new StringBuffer();
            //刷新购物车
        	List<Product> productList = new ArrayList<Product>();
            List<Product> userFavoritesList = userFavoritesJdbc.findAll(user.getId());
            for (Product product : userFavoritesList) {
                Product productTemp = AuctionUtil.getById(product.getId());
                if (null != productTemp) product = productTemp;
                
                if(user.getId()==product.getBidUserId())
                {
                	product.setBidUserName("我");
                }
    			
                productList.add(product);
            	idSb.append(product.getId() + ",");
            }
            this.setRequest("userFavoritesList", productList);
            List<PayParameter> payParameterList = this.payParameterJdbc.findAll();
            this.setRequest("payParameterList", payParameterList);
    		if(user!=null)
    		{
    			UserAccount userAccount =  userAccountJdbc.getByUserId(user.getId());
    			this.setRequest("userAccount", userAccount);
    		}
    		String lastIdStr = "";
    		if (idSb.length() > 0) {
    			lastIdStr = idSb.substring(0, idSb.length() - 1);
    		}
    		this.setRequest("lastIdStr", lastIdStr);
        }

        this.forward("/user/user-shoppingcart.jsp");
    }
    
    public void getUserBidding() throws Exception
    {
    	User user = this.getCurrentUser();
    	if(user==null)
    		this.print("no");
    	
    	UserAccount userAccount =  userAccountJdbc.getByUserId(user.getId());
    	if(userAccount==null)
    		this.print("no");
		this.print(userAccount.getBiddingCount()+"");
    }
}
